package 字节码文件.类型转换指令;

import org.junit.Test;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 宽化类型转换  窄化类型转换
 * <P> Date: 2022/3/26:23:17
 * <P> Description:
 */

public class ClassCastTest {
    /**
     * TODO:宽化类型转换
     **/
    public void upCase1() {
        int i = 10;
        long l = i;
        float f = i;
        double d = i;

        float f1 = l;
        double d1 = l;

        double d2 = f1;
    }

    /**
     * TODO:精度损失问题
     **/
    @Test
    public void upCase2() {
        int i = 123123123;
        float f = i;
        //1.2312312E8 123123120 损失了
        System.out.println(f);

        long l = 123123123123l;
        l = 123123123123123123l;
        double d = l;
        //1.2312312312312312E17 123123123123123120
        System.out.println(d);
    }

    /**
     * TODO:针对byte short 等转换为大容量的类型
     **/
    public void upCase3(byte b) {
        int i = b;
        long l = b;
        double d = b;
    }

    public void upCase4(short b) {
        int i = b;
        long l = b;
        double d = b;
    }


    /**
     * TODO:窄化类型转换
     **/
    public void downCast1() {
        int i = 10;
        byte b = (byte) i;
        short s = (short) i;
        char c = (char) i;

        long l = 10L;
        int i1 = (int) l;
        byte b1 = (byte) l;
    }

    public void downCast2() {
        float f = 10;
        long l = (long) f;
        int i = (int) f;
        byte b = (byte) f;

        double d = 10;
        byte b1 = (byte) d;
    }

    /**
     * TODO: 0 bipush 10
     * 2 istore_1
     * 3 iload_1
     * 4 i2b
     * 5 istore_2
     * 6 return
     **/
    public void downCast3() {
        short s = 10;
        byte b = (byte) s;
    }

    /**
     * TODO:窄化类型精度损失
     **/
    @Test
    public void downCast4() {
        int i = 128;
        byte b = (byte) i;
        //-128
        System.out.println(b);
    }

    @Test
    public void downCast5() {
        double d1 =Double.NaN;
        int i  = (int) d1;
        System.out.println(i);

        double d2 = Double.POSITIVE_INFINITY;
        long l = (long) d2;
        int j = (int) d2;
        System.out.println(l);
        System.out.println(j);

        float f = (float) d1;
        float f1 = (float) d2;
        System.out.println(f);
        System.out.println(f1);
        //0
        //9223372036854775807
        //2147483647
        //NaN
        //Infinity
    }
}
